package com.lagou.rpc.provider;

import com.lagou.rpc.provider.server.RpcServer;
import com.lagou.rpc.provider.server.ZkServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.net.SocketException;

@SpringBootApplication
public class ServerBootstrapApplication implements CommandLineRunner {


    @Autowired
    private RpcServer rpcServer;

    @Autowired
    private ZkServer zkServer;

    @Value("${rpcserver.port}")
    private int port;

    public static void main(String[] args) {
        SpringApplication.run(ServerBootstrapApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    zkServer.registerToZk();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                try {
                    rpcServer.startServer(zkServer.getRealIp(), port);
                } catch (SocketException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

}
